package com.worthtech.billportal.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

import jxl.Workbook;

public class InputExcel {
	public static boolean isNumeric(String str){
	     Pattern pattern = Pattern.compile("[0-9]*");
	     return pattern.matcher(str).matches();   
	}
	public static boolean InputExcel(String title,HashMap map){
		String url="./webapps/billportal2/"+title+".xls";
		try {
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(url));
			OutputStream os = new FileOutputStream(url);
			System.out.println(url);
			wwb = Workbook.createWorkbook(os);
			jxl.write.WritableSheet ws = wwb.createSheet(title, 0);
			for(int i=0;i<map.size();i++){
				String []str=(String[]) map.get(String.valueOf(i));
				for(int j=0;j<str.length;j++){
//					if(isNumeric(str[j])){
//						System.out.println(str[j]);
//						jxl.write.Number label = new jxl.write.Number(j, i, Integer.valueOf(str[j]).intValue());
//						ws.addCell(label);
//					}else{
						jxl.write.Label label= new jxl.write.Label(j, i, str[j]);
						ws.addCell(label);
//					}
				}
			}
			wwb.write();
			wwb.close();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	public static void InputExcel(String title,Collection c){
		HashMap map=new HashMap();
		Iterator i = c.iterator();
		int p=0;
		while(i.hasNext()){
			Object o= i.next();
			String [] str = o.toString().split(",");
//			System.out.println(p+" "+str[0]);
			map.put(String.valueOf(p), str);
//			System.out.println(map.size());
			p++;
		}
		InputExcel(title,map);
	}
}
